# -*- coding: utf-8 -*-

"""
针对django框架的封装,使用json+base64
"""
import json
from django.conf import settings
from tools.crypt_tools.aes import ECBPrpcrypt

AES_KEY = settings.AES_KEY

class Prpcrypt(object):

    local_prpcrypt = ECBPrpcrypt(key=AES_KEY)

    @classmethod
    def encrypt(cls, message):
        """
        加密函数
        :param text: 明文
        :return: 密文
        """
        try:
            m = json.dumps(message)
        except Exception:
            raise ValueError("数据不合法：".format(message))
        else:
            return cls.local_prpcrypt.encrypt(m)

    @classmethod
    def decrypt(cls, message):
        """
        @desc 解密密文转成json
        :param message:
        :return:
        """
        try:
            m = cls.local_prpcrypt.decrypt(message)
            res = json.loads(m)
        except Exception as e:
            raise ValueError("解密失败:".format(e))
        return res


prpcrypt_manager = Prpcrypt